package cloudUser.service.impl;

import cloudEntity.dto.user.SmartUserDTO;
import cloudEntity.dto.user.SmartUserRegisterDTO;
import cloudEntity.entity.SmartUserEntity;
import cloudEntity.vo.PageBeanVO;
import cloudEntity.vo.news.SmartNewsVO;
import cloudEntity.vo.user.SmartUserLoginVO;
import cloudEntity.vo.user.SmartUserVO;
import cloudUser.config.CustomerUserDetailsService;
import cloudUser.mapper.SmartUserMapper;
import cloudUser.service.SmartUserService;
import cloudUser.util.IPUtils;
import cloudUser.util.JwtUtilCustomer;
import cloudUser.util.RedisUtil;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: Smart School
 * @ClassName SmartUserServiceImpl
 * @description: SmartUserService 实现 类;
 * @author: 陈智博
 * @create: 2025-04-06 14:21
 * @Version 1.0
 **/
@Service
public class SmartUserServiceImpl implements SmartUserService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SmartUserMapper smartUserMapper;

    @Override
    public SmartUserLoginVO login(SmartUserDTO smartUserDTO,
                                  HttpServletRequest request) {

        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken
                = new UsernamePasswordAuthenticationToken(smartUserDTO.getSysUserUsername(),smartUserDTO.getSysUserPassword());

        Authentication authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);

        //获取返回用户的信息
        CustomerUserDetailsService customerUserDetailsService = (CustomerUserDetailsService) authenticate.getPrincipal();

        SmartUserEntity smartUserEntity = customerUserDetailsService.getSmartUserEntity();

        //转换 VO
        SmartUserLoginVO smartUserLoginVO = new SmartUserLoginVO();
        BeanUtils.copyProperties(smartUserEntity,smartUserLoginVO);

        Map<String,Object> map = new HashMap<>();
        map.put("userUid",smartUserEntity.getSysUserUid());

        //返回token
        smartUserLoginVO.setToken(JwtUtilCustomer.createToken(map,"用户登录."));
        System.out.println(request.toString());
        //获取IP信息
        smartUserLoginVO.setIpAddress(IPUtils.getIpAddress(request));
        smartUserLoginVO.setLocation(IPUtils.getIpRegion(IPUtils.getIpAddress(request)));
        smartUserLoginVO.setBrower(IPUtils.getBrowser(request));

        //把 Entity 实体类 转换为 String;
        String jsonStr = JSONUtil.toJsonStr(customerUserDetailsService);

        //设置 数据保存到redis中.
        redisUtil.set("login"+smartUserEntity.getSysUserUid(),jsonStr);

        return smartUserLoginVO;
    }

    @Override
    public PageBeanVO<SmartUserVO> GetSmartUserReturnPage(Integer type, Integer pageNum, Integer pageSize) {
        Integer pageBean = (pageNum - 1) * pageSize;
        Integer countSize = smartUserMapper.getUserCountByType(type);
        List<SmartUserVO> smartUserByPageInfo = smartUserMapper.getSmartUserByPageInfo(type, pageBean, pageSize);
        return new PageBeanVO<>(pageNum,pageSize,countSize,smartUserByPageInfo);
    }

    @Override
    public Integer RegisterUser(SmartUserRegisterDTO smartUserRegisterDTO) {
        return smartUserMapper.RegisterUser(smartUserRegisterDTO);
    }

}
